﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Threading;
using DomusDotNet.Sbm.Client.SL4noCM.Infrastructure;
using DomusDotNet.Sbm.Client.SL4noCM.Services;
using DomusDotNet.Sbm.Client.SL4noCM.WCFServices;

using System.Linq;
namespace DomusDotNet.Sbm.Client.SL4noCM.ViewModels
{
    public class LoginViewModel : ViewModelBase
    {
        public LoginViewModel()
        {
            if (webService != null)  webService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(LoginCompleted);
        }

        private void LoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            WebRequestExecution = false;
            if (e.Error != null)
            {
                ErrorMessage = e.Error.Message;
            }
            else
            {
                if (e.Result.Result)
                {
                    App.SecurityContext = new DefaultSecurityContext() { SessionGUID = e.Result.SessionGUID };
                    App.Messenger.Send<String>(Messages.Login);
                }
                else
                {
                    ErrorMessage = e.Result.Errors[0].Message;
                }
            }
        }

        string username;
        public string Username
        {
            get
            {
                return username;
            }
            set
            {
                if (username != value)
                {
                    username = value;
                    OnPropertyChanged("Username");
                    ErrorMessage = string.Empty;
                }
            }
        }

        string password;
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                if (password != value)
                {
                    password = value;
                    OnPropertyChanged("Password");
                    ErrorMessage = string.Empty;
                }
            }
        }

        private RelayCommand loginCommand;
        public ICommand LoginCommand
        {
            get
            {
                if (loginCommand == null)
                {
                    loginCommand = new RelayCommand(
						(a) => Login(null),
                        (b) => CanLogin(null)
					).ListenTo(this, "Username", "Password");
                }
                return loginCommand;
            }
        }


        public bool CanLogin(object obj)
        {
            return (!string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password));
        }

        public void Login(object obj)
        {
            ErrorMessage = string.Empty;
            var loginRequest = new LoginRequest() { UserName = this.Username, Password = this.Password };
            webService.LoginAsync(loginRequest);
            WebRequestExecution = true;
        }

    }
}
